#!/bin/bash

# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# ----------------------------------------- 环境变量 -----------------------------------------
# 根目录
ROOT_DIR=`pwd` 

# 获取脚本的绝对路径
script_path=$(readlink -f "${BASH_SOURCE}" 2>/dev/null || readlink "${BASH_SOURCE}" 2>/dev/null || echo "${BASH_SOURCE}")
script_dir=$(dirname "$script_path")
# 导入文件
source $script_dir/check_env.sh

# ohpm路径
OHPM_FILE_DIR=$script_dir/command-line-tools/ohpm/bin
# hvigorw路径
HVIGORW_CMD=$script_dir/command-line-tools/hvigor/bin/hvigorw
# 输出文件路径
HAP_FILE_DIR=$ROOT_DIR/out/codelabs/packages
mkdir -p $HAP_FILE_DIR
# SDK路径
DEVECO_SDK_HOME=$script_dir/command-line-tools/sdk
# 设置SDK环境变量
export DEVECO_SDK_HOME=${DEVECO_SDK_HOME}
export PATH=$DEVECO_SDK_HOME:$PATH

# ----------------------------------------- 函数 -----------------------------------------
function ohpm_init() {
    echo -e "---------------------------------ohpm_init start---------------------------------"

    # 初始化ohpm
    cd $OHPM_FILE_DIR
    echo 'bash init:'
    bash init
    if [ $? -eq 0 ]
    then
        # 安装git
        echo 'ohpm init success'
    fi
    cd $OLDPWD

    echo -e "---------------------------------ohpm_init finish---------------------------------\n"
}

function build() {
    echo -e "---------------------------------build start---------------------------------"
    
    # 进入项目目录，$1 首个参数
    cd $1

    # 判断工程级目录下是否存在oh-package.json5
    if [ -e "oh-package.json5" ]
    then
        # 查找所有oh-package.json5文件，执行ohpm install安装依赖
        for path in $(find . -name "oh-package.json5")
        do
            path_array=(${path//\// })
            lenth=${#path_array[@]}
            if [ $lenth -eq 3 ]
            then
                cd ${path_array[1]}
                $OHPM_FILE_DIR/ohpm install
                cd $OLDPWD
            fi
        done
        $OHPM_FILE_DIR/ohpm install

        local cmd=./hvigorw
        if [ ! -f $cmd ]
        then
            cmd=$HVIGORW_CMD
        fi
        
        # 执行编译构建
        bash $cmd --mode module assembleHap assembleHar assembleHsp --no-daemon
        res=$?
        if [ $res != 0 ]
        then
            # 编译构建失败，输出日志直接退出脚本
            echo "$1 build failed"
            exit 1
        fi
    else
        echo "can not support this project! miss package.json or oh-package.json5"
        exit 1
    fi

    # 移动输出文件
    # if [ $res -eq 0 ]
    # then
    #     for path in $(find . -name build-profile.json5 | grep -v "\./build-profile.json5" | grep -v "ohpm/" | grep -v "/build/")
    #     do
    #         path=${path:2}
    #         filepath=(${path//\// })
    #         cp -arf $1/$filepath/build/default/outputs $HAP_FILE_DIR
    #         if [ $? -ne 0 ]
    #         then
    #             res=1
    #             break
    #         fi
    #     done
    # fi
    echo -e "---------------------------------build finish---------------------------------\n"
}

function start_build() {
    echo -e "\n---------------------------------start_build start---------------------------------"

    local res=0
    if [ -z $COMPONT_NAME ]
    then
        echo "have not get the compont!"
        return 1
    fi

    # 根目录/仓库名
    PROJECT_DIR=$ROOT_DIR/$COMPONT_NAME

    if [ -z "$PROJECT_NAMES" ]
    then
        echo "build singel project"
        build $PROJECT_DIR
    else 
        echo "build multi project"
        # 支持多工程，遍历多仓工程名
        for project_name in $PROJECT_NAMES
        do
            # 拼接工程目录，执行编译构建
            build $PROJECT_DIR/$project_name
        done
    fi
    
    cd $ROOT_DIR
    echo -e "\n---------------------------------start_build finish---------------------------------"

    return $res
}

function compile() {
    echo -e "\n---------------------------------compile start---------------------------------"
    
    start_build $1
    if [ $? -ne 0 ]
    then
        echo "############################ start_build failed ############################"
        return 1
    fi

    echo -e "---------------------------------compile finish---------------------------------\n"

    return 0
}

# ----------------------------------------- 执行入口 -----------------------------------------
cd $script_dir
prepare $*
if [ $? -ne 0 ]
then
    echo "prepare CI compile Env failed!"
    exit 1
fi

compile $*
exit $?

#eof
